<template>
	<view class="collection-index border-box full-height">
		<!-- <u-navbar title="轻松小镇" bgColor="#ffffff" leftIconColor="#ffffff" safeAreaInsetTop :placeholder="false"
		titleStyle="{fontSize: 16px;font-weight: bold;color: #000000;}"
		class="navbar" :class="isScroll ? 'navbar-show' : 'navbar-hide'">
		</u-navbar> -->
		<u-swiper :list="bannerList" indicator indicatorMode="line" @click="clickBanner" keyName="image" circular
			id="mySwiper" :loading="isLoading" height="526rpx" radius="0"
			:indicatorStyle="{'left': '40rpx','bottom':'30rpx'}">
		</u-swiper>
		<u-sticky :offset-top="setTop">
			<view class="title flex-no-horizontal p-20" id="tabs">
				<u-tabs :list="tabList" lineWidth="0" lineHeight="0"
					itemStyle="padding: 0;width: 180rpx;justify-content: flex-start;"
					inactiveStyle="font-size: 28rpx;color: #8F9091;" @click="changeTab"
					activeStyle="font-weight: bold;font-size: 36rpx;color: #000000;"></u-tabs>
				<view class="flex-no-horizontal" @click="toPage">
					<view class="f-base">查看我的藏品</view>
					<u-icon name="arrow-right" size="10" color="#333333"></u-icon>
				</view>
			</view>
		</u-sticky>
		<z-paging ref="paging" v-model="list" @query="queryList" @scroll="scroll" :auto-show-back-to-top="true"
			:auto-clean-list-when-reload="false" :auto-scroll-to-top-when-reload="false" :use-page-scroll="true">
			<view class="box-pading">
				<view class="list">
					<view class="list-item m-b-15" v-for="item in list" :key="item.id"
						@click="toActivity(item.status, item.link)">
						<view class="item-up">
							<template v-if="item.isDisplayTime">
								<view class="time flex-no-horizontal reserve" v-if="item.status === 1">
									<u-icon name="clock" size="11" color="#4ABC5A"></u-icon>
									<text class="f-xxs green p-row-5">距预约结束 </text>
									<u-count-down :time="(item.reserveEndTime - item.systemTime) * 1000"
										format="DD:HH:mm:ss"></u-count-down>
								</view>

								<view class="time flex-no-horizontal buy" v-else-if="item.status === 2">
									<u-icon name="clock" size="11" color="#FF8E2A"></u-icon>
									<text class="f-xxs orange p-row-5">距抢购开始 </text>
									<u-count-down :time="(item.snapUpStartTime - item.systemTime) * 1000"
										format="DD:HH:mm:ss"></u-count-down>
								</view>

								<view class="time flex-no-horizontal buy" v-else-if="item.status === 3">
									<u-icon name="clock" size="11" color="#FF8E2A"></u-icon>
									<text class="f-xxs orange p-row-5">距抢购结束 </text>
									<u-count-down :time="(item.snapUpEndTime - item.systemTime) * 1000"
										format="DD:HH:mm:ss"></u-count-down>
								</view>

								<view class="time flex-no-horizontal" v-else-if="item.status === 0">
									<text class="f-xxs grey p-row-5">已结束</text>
								</view>
							</template>
							<view class="time flex-no-horizontal reserve" v-else-if="item.displayContent">
								<text class="f-xxs orange">{{item.displayContent}}</text>
							</view>
							<u--image :src="item.banner" height="320rpx" width="100%" :lazy-load="true"></u--image>
						</view>
						<view class="item-down">
							<view class="bold f-large m-b-15">{{item.title}}</view>
							<view class="flex-no-horizontal">
								<view class="producer flex-no-horizontal">
									<u--image :src="item.avatar" shape="circle" width="40rpx" height="40rpx"
										mode="scaleToFill" class="producer-icon"></u--image>
									<text class="f-xs u-line-1">{{item.publisher}}</text>
								</view>
								<view class="number flex-no-horizontal">
									<text class="tag1">限量</text>
									<text class="tag2">{{item.publishNum}} 枚</text>
								</view>
							</view>
							<u-divider :hairline="true" lineColor="rgba(0, 0, 0, 0.05)"></u-divider>
							<view class="f-xs grey">{{item.description}}</view>
							<u-button shape="circle" block type="success" v-if="item.status === 1" class="m-t-15"
								color="#4ABC5A">立即预约</u-button>

							<u-button shape="circle" block class="m-t-15" v-else-if="item.status === 2" color="#F7F7F7"
								customStyle="color: #8F9091;">等待发售</u-button>

							<u-button shape="circle" block type="warning" v-else-if="item.status === 3" class="m-t-15"
								color="#FF8E2A">立即抢购</u-button>

							<u-button shape="circle" customStyle="color: #8F9091;" block v-else-if="item.status === 0"
								:disabled="true" class="m-t-15" color="#F7F7F7">已结束</u-button>

							<view class="f-xs text-center orange m-t-10" v-if="item.status">参与人数：{{item.reserveNum}}
							</view>
							<view class="f-xs text-center grey m-t-10" v-else>参与人数：{{item.reserveNum}}</view>
						</view>
					</view>
				</view>
			</view>

		</z-paging>
		<view class="attested" v-if="!user.isAttested">
			<view class="flex-no-horizontal attested-wrap">
				<view>
					<view class="f-base">完成实名认证，抢购快人一步</view>
					<view class="f-xxs" style="color: #F7F7F7;">根据相关法律规定 完成实名才可获得数字藏品</view>
				</view>
				<view class="attested-btn text-center f-s" @click="toAttested">去实名</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		jump
	} from '../../utility/bannerJump.js'
	import {
		activityList,
		bannerList
	} from '../../api/collection.js'
	import {
		ifLoginFun
	} from '../../utility/weChatLogin.js'
	import tabbar from '../../common/tabbar.vue'
	import {
		mapState
	} from 'vuex'
	import ZPagingMixin from '@/uni_modules/z-paging/components/z-paging/js/z-paging-mixin'
	export default {
		mixins: [ZPagingMixin],
		data() {
			return {
				bannerList: [],
				tabList: [{
					name: '正在上线',
					value: 1
				}, {
					name: '往期上线',
					value: 0
				}],
				tabsIndex: 1, // 1是正在上线 2已上线
				list: [],
				isLoading: true,
				isScroll: false,
				setTop: 0,
				swiperHeight: 0,
				sticky: false
			}
		},
		computed: {
			...mapState({
				user: state => state.userInfo
			})
		},
		onLoad() {
			this.getBanner()
			const res = uni.getSystemInfoSync()
			this.setTop = res.statusBarHeight * 2
		},
		onPageScroll(e) {
			const query = uni.createSelectorQuery().in(this);
			query.select('#tabs').boundingClientRect(data => {
				data.top === this.setTop ? this.sticky = true : this.sticky = false
			}).exec();
			if (e.scrollTop === 0) {
				this.$refs.paging.reload(true)
			}
		},
		onReady() {
			this.getOffsetTop()
		},
		methods: {
			getOffsetTop() {
				const query = uni.createSelectorQuery().in(this);
				query.select('#mySwiper').boundingClientRect(data => {
					this.swiperHeight = data.height
				}).exec();
			},
			//下拉刷新,上拉加载
			async queryList(pageNo, pageSize) {
				try {
					let params = {
						pageNum: pageNo,
						pageSize,
						status: this.tabsIndex
					}
					const res = await activityList({
						...params
					})
					this.list = res.data
					this.$refs.paging.complete(this.list);
				} catch (e) {
					//TODO handle the exception
					// 如果请求失败
					this.$refs.paging.complete(false) //展示错误页面
				}
				//这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				//这里的请求只是演示，请替换成自己的项目的网络请求，并在网络请求回调中通过this.$refs.paging.complete(请求回来的数组)将请求结果传给z-paging
				// this.$request.queryList({
				// 	pageNo,
				// 	pageSize
				// }).then(res => {
				// 	//请勿在网络请求回调中给dataList赋值！！只需要调用complete就可以了
				// 	this.$refs.paging.complete(res.data.list);
				// }).catch(res => {
				// 	//如果请求失败写this.$refs.paging.complete(false)，会自动展示错误页面
				// 	//注意，每次都需要在catch中写这句话很麻烦，z-paging提供了方案可以全局统一处理
				// 	//在底层的网络请求抛出异常时，写uni.$emit('z-paging-error-emit');即可
				// 	this.$refs.paging.complete(false);
				// })
			},
			scroll(e) {
				this.isScroll = e.detail.scrollTop
			},
			async getBanner() {
				try {
					const res = await bannerList()
					this.bannerList = res.data
					this.isLoading = false
				} catch (e) {
					this.isLoading = false
					uni.showToast({
						title: e.data || e.message,
						icon: 'none'
					})
					console.log(e)
				}
			},
			clickBanner(index) {
				jump(this.bannerList[index])
			},
			changeTab(item) {
				this.tabsIndex = item.value
				if (this.sticky) {
					uni.pageScrollTo({
						scrollTop: this.swiperHeight - this.setTop
					})
				}
				this.$refs.paging.reload()
			},
			toPage() {
				if (!ifLoginFun()) {
					return false
				}
				uni.navigateTo({
					url: './myCollection',
				})

			},
			toActivity(status, link) {
				if (!status) {
					return false
				}
				if (!ifLoginFun()) {
					return false
				}
				if (link) {
					let url = ''
					const token = this.$store.state.collectionToken
					const time = new Date().getTime();
					if (link.indexOf('#') > -1 && token) {
						url = `${link.split('#')[0]}?token=${token}#${link.split('#')[1]}&refresh=${time}`
					} else {
						url = `${link}?acctoken=${token}`
					}
					uni.navigateTo({
						url: `./otherPage?url=${url}`
					})
				} else {
					uni.showToast({
						title: '暂无跳转地址',
						icon: 'none'
					})
				}
			},
			toAttested() {
				if (!ifLoginFun()) {
					return false
				}
				uni.navigateTo({
					url: '../user/realName'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.collection-index {
		font-size: $uni-font-size-base;
		background-color: #FFFFFF;

		::v-deep {
			.z-paging-content {
				height: auto;
			}
		}

		.navbar ::v-deep {
			.u-navbar--fixed {
				transition: all .5s;
			}
		}

		.navbar-show ::v-deep {
			.u-navbar--fixed {
				opacity: 1;
			}
		}

		.navbar-hide ::v-deep {
			.u-navbar--fixed {
				opacity: 0;
			}
		}

		.orange {
			color: #FF8E2A;
		}

		.green {
			color: #4ABC5A;
		}

		.grey {
			color: #8F9091;
		}

		.title {
			justify-content: space-between;
			background-color: #FFFFFF;
		}

		.list {
			padding-top: 20rpx;

			.list-item {
				background: #FFFFFF;
				box-shadow: 0 4rpx 125rpx 0 rgba(0, 0, 0, 0.1);
				border-radius: 20rpx;
				overflow: hidden;

				.item-up {
					position: relative;
					height: 320rpx;
					z-index: 0;
					overflow: hidden;

					.time {
						position: absolute;
						background: #000000;
						opacity: 0.8;
						font-size: 22rpx;
						padding: 12rpx 20rpx;
						left: 20rpx;
						top: 20rpx;
						z-index: 1;
						border-radius: 48rpx;

						::v-deep .u-count-down__text {
							font-size: 12px;
						}
					}

					.reserve {
						::v-deep .u-count-down__text {
							color: #4ABC5A;
						}
					}

					.buy {
						::v-deep .u-count-down__text {
							color: #FF8E2A;
						}
					}
				}

				.item-down {
					padding: 30rpx;

					.producer {
						width: 65%;
						margin-right: 20rpx;

						.producer-icon {
							width: 40rpx;
							height: 40rpx;
							background: #3676EC;
							border-radius: 50%;
							margin-right: 15rpx;
						}
					}

					.number {
						.tag1 {
							background: #000000;
							border-radius: 4rpx 0 0 4rpx;
							padding: 0 4rpx;
							font-size: 10px;
							color: white;
						}

						.tag2 {
							background: #F7F7F7;
							border-radius: 0 4rpx 4rpx 0;
							padding: 0 4rpx;
							font-size: 10px;
						}
					}
				}
			}

			.over {
				filter: grayscale(1);
			}
		}

		.attested {
			height: 126rpx;

			.attested-wrap {
				position: fixed;
				bottom: 0;
				left: 0;
				width: 100vw;
				background: #000000;
				z-index: 10;
				height: 126rpx;
				justify-content: space-between;
				color: white;
				padding: 0 40rpx;
				box-sizing: border-box;

				.attested-btn {
					width: 172rpx;
					height: 64rpx;
					background: linear-gradient(16deg, #FFE8E2, #FFE09E);
					border-radius: 16px;
					line-height: 64rpx;
					color: #000000;
				}
			}
		}
	}
</style>
